package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.common.CommonService;
import tt.dz.common.BusinessException;
import tt.dz.common.DzConstants;
import tt.dz.common.DzConstants.bizSysCode;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.ExceptionErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.object.Node;

@Service("rpcGetDistrictService")
public class RpcGetDistrictService
  implements RPCBaseService
{
  @Resource(name="commonService")
  private CommonService commonService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    String resJson = null;
    try
    {
      String name = message.getName();
      String reqJson = ((GenericRecord)request).get("req").toString();
      if ("listNextDistrictByDistrictCode".equals(name)) {
        resJson = listNextDistrictByDistrictCode(reqJson);
      } else if ("searchAllClassificationAndAssembly".equals(name)) {
        resJson = searchAllClassificationAndAssembly(reqJson);
      } else if (resJson == null) {
        resJson = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.value(), DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION.getDesc()).toString();
      }
    }
    catch (BusinessException e)
    {
      resJson = RPCUtil.generateResponseObject(Integer.valueOf(0), e.getStatusCode(), e.getExceptionMsg()).toString();
    }
    catch (Exception e)
    {
      DzErrorCode.ExceptionErrorCode ec = DzErrorCode.ExceptionErrorCode.COMMON_EXCEPTION;
      
      resJson = RPCUtil.generateResponseObject(Integer.valueOf(0), ec.value(), ec.getDesc()).toJSONString();
    }
    return resJson;
  }
  
  private String searchAllClassificationAndAssembly(String reqJson)
    throws Exception
  {
    JSONObject resObj = null;
    JSONObject reqObj = JSON.parseObject(reqJson);
    String sysCode = reqObj.getString("sysCode");
    if (StringUtils.isBlank(sysCode))
    {
      DzErrorCode.CommonErrorCode e = DzErrorCode.CommonErrorCode.PARAM_REQUIRED;
      throw new BusinessException(e.value(), e.getDesc(), "必传参数系统码sysCode不能为空");
    }
    boolean isExistFlag = false;
    for (DzConstants.bizSysCode v : DzConstants.bizSysCode.values()) {
      if (v.value().equals(sysCode))
      {
        isExistFlag = true;
        break;
      }
    }
    if (!isExistFlag)
    {
      DzErrorCode.CommonErrorCode e = DzErrorCode.CommonErrorCode.SYSCODE_NOT_FOUNT;
      throw new BusinessException(e.value(), e.getDesc(), "系统编码不存在");
    }
    Node node = this.commonService.searchAllClassificationAndAssembly();
    resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
    resObj.put("result", node);
    return JSON.toJSONString(resObj);
  }
  
  private String listNextDistrictByDistrictCode(String reqJson)
    throws Exception
  {
    JSONObject resObj = null;
    JSONObject reqObj = JSON.parseObject(reqJson);
    String districtCode = reqObj.getString("districtCode");
    String level = reqObj.getString("level");
    if (!StringUtils.isNotBlank(districtCode))
    {
      DzErrorCode.CommonErrorCode e = DzErrorCode.CommonErrorCode.PARAM_REQUIRED;
      throw new BusinessException(e.value(), e.getDesc(), "必传参数区域码districtCode不能为空");
    }
    JSONObject result = this.commonService.listNextDistrictByDistrictCode(districtCode, level);
    resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
    resObj.put("result", result);
    return JSON.toJSONString(resObj);
  }
}
